#!/usr/bin/env bash
#

# НЕ ВКЛЮЧЕН, так как в старой версии установщика (arch-installer-old) я мог выбрать сборщик через "pacstrap -i"
# теперь такой возможности нет, хз теперь как...
while true; do
    echo -en "----------------------------------------------------------------------\n"
    echo -en "| Выберите инструмент для создания образа initramfs\n"
    echo -en "| Оставьте поле пустым для выбора по умолчанию: ${GREEN}mkinitcpio\n${NORMAL}"
    echo -en "----------------------------------------------------------------------\n"
    echo -en "| 1 - mkinitcpio\n| 2 - dracut\n"
    echo "^^^^^^^^^^^^^^^^^^^^^^"
    read -r -p "| Ваш выбор (1/2): >>> " tool_type
    case ${tool_type} in
        1 | "")
            echo "| Ваш выбор - mkinitcpio"
            break;;
        2)
            echo "| Ваш выбор - dracut"
            mkdir -p /mnt/usr/local/bin
            mkdir -p /mnt/etc/pacman.d/hooks
            touch /mnt/usr/local/bin/dracut-install.sh
            touch /mnt/usr/local/bin/dracut-remove.sh
            touch /mnt/etc/pacman.d/hooks/90-dracut-install.hook
            touch /mnt/etc/pacman.d/hooks/60-dracut-remove.hook
            {
                echo "#!/usr/bin/env bash"
                echo ""
                echo "args=('--force' '--no-hostonly-cmdline')"
                echo ""
                echo "while read -r line; do"
                echo "    if [[ \"\$line\" == 'usr/lib/modules/'+([^/])'/pkgbase' ]]; then"
                echo "        read -r pkgbase < \"/\${line}\""
                echo "        kver=\"\${line#'usr/lib/modules/'}\""
                echo "        kver=\"\${kver%'/pkgbase'}\""
                echo ""
                echo "        install -Dm0644 \"/\${line%'/pkgbase'}/vmlinuz\" \"/boot/vmlinuz-\${pkgbase}\""
                echo "        dracut \"\${args[@]}\" --hostonly \"/boot/initramfs-\${pkgbase}.img\" --kver \"\$kver\""
                echo "        dracut \"\${args[@]}\" --no-hostonly \"/boot/initramfs-\${pkgbase}-fallback.img\" --kver \"\$kver\""
                echo "    fi"
                echo "done"
            } >> /mnt/usr/local/bin/dracut-install.sh
            #-------
            {
                echo "#!/usr/bin/env bash"
                echo ""
                echo "while read -r line; do"
                echo "    if [[ \"\$line\" == 'usr/lib/modules/'+([^/])'/pkgbase' ]]; then"
                echo "        read -r pkgbase < \"/\${line}\""
                echo "        rm -f \"/boot/vmlinuz-\${pkgbase}\" \"/boot/initramfs-\${pkgbase}.img\" \"/boot/initramfs-\${pkgbase}-fallback.img\""
                echo "    fi"
                echo "done"
            }  >> /mnt/usr/local/bin/dracut-remove.sh
            #-------
            {
                echo "[Trigger]"
                echo "Type = Path"
                echo "Operation = Install"
                echo "Operation = Upgrade"
                echo "Target = usr/lib/modules/*/pkgbase"
                echo ""
                echo "[Action]"
                echo "Description = Updating linux initcpios (with dracut!)..."
                echo "When = PostTransaction"
                echo "Exec = /usr/local/bin/dracut-install.sh"
                echo "Depends = dracut"
                echo "NeedsTargets"
            } >> /mnt/etc/pacman.d/hooks/90-dracut-install.hook
            #-------
            {
                echo "[Trigger]"
                echo "Type = Path"
                echo "Operation = Remove"
                echo "Target = usr/lib/modules/*/pkgbase"
                echo ""
                echo "[Action]"
                echo "Description = Removing linux initcpios..."
                echo "When = PreTransaction"
                echo "Exec = /usr/local/bin/dracut-remove.sh"
                echo "NeedsTargets"
            } >> /mnt/etc/pacman.d/hooks/60-dracut-remove.hook
            #-------
            chmod +x /mnt/usr/local/bin/dracut-install.sh
            chmod +x /mnt/usr/local/bin/dracut-remove.sh
            chmod +x /mnt/etc/pacman.d/hooks/90-dracut-install.hook
            chmod +x /mnt/etc/pacman.d/hooks/60-dracut-remove.hook
            break;;
        *)
            echo "| Неверный ввод, попробуйте снова..."
            continue;;
    esac
done
